//
//  JXHomeController.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/2/26.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit




class JXHomeController: JXBaseTableViewController {

    
    
    var statuses : [JXStatus]? {
        didSet {
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        

        if !JXUserAcountViewModel.sharedInstance.isUserLogin {
            return
        }
        
        
        tableView.registerClass(JXStatusCell.self, forCellReuseIdentifier: "cell")
        
        statusVM.loadStatus { (statuses, error) -> () in
            if error != nil {
                print("获取微博数据失败:\(error)")
                return
            }
            self.statuses = statuses
//            print("\n\t获取微博数据成功:\(statuses)")
        }
        
        setupNavigation()
        
        tableView.estimatedRowHeight = 150
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.allowsSelection = false
    }
    
    
    private lazy var statusVM : JXStatusViewModel = JXStatusViewModel()
    
    private func setupNavigation () {
        setupBarButtonItem()
        setupTitle()
    }
    
    private func setupBarButtonItem () {
//        let leftButton = UIButton()
//        leftButton.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
//        leftButton.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
//        leftButton.sizeToFit()
//        
//        let rightButton = UIButton()
//        rightButton.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
//        rightButton.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
//        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
//        rightButton.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_pop")
    }
    
    private func setupTitle () {
        
//         let titleString = JXUserAcountViewModel.sharedInstance.userAcount?.screen_name
        
        let button = JXTitleButton(title: "我是标题", imageName: "navigationbar_arrow")
        
        navigationItem.titleView = button
        
        button.addTarget(self, action: "titleButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    func titleButtonClick (button : UIButton) {
        
        button.selected = !button.selected
        
        if button.selected {
            let sb = UIStoryboard.init(name: "Popover", bundle: nil)
            let popoverVC = sb.instantiateViewControllerWithIdentifier("popover")
            popoverVC.modalPresentationStyle = UIModalPresentationStyle.Popover
            // 设置popoverPresentationController代理
            popoverVC.popoverPresentationController?.delegate = self
            popoverVC.popoverPresentationController?.sourceView = navigationItem.titleView
            popoverVC.popoverPresentationController?.sourceRect = navigationItem.titleView!.bounds
            
            // 设置弹出控制器显示的大小
            popoverVC.preferredContentSize = CGSize(width: 200, height: 5 * 44)
            
            presentViewController(popoverVC, animated: true, completion: nil)
        }
    }
    
}

extension JXHomeController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! JXStatusCell
        
//        cell.textLabel?.text = statuses?[indexPath.row].text
        cell.status = statuses?[indexPath.row]
        
        
        return cell
    }
    
}



extension JXHomeController: UIPopoverPresentationControllerDelegate {
    // 让iPhone上面可以显示popover效果
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    // popover关闭时调用
    func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
        // 将标题按钮的箭头转下去
        let button = navigationItem.titleView as! JXTitleButton
        titleButtonClick(button)
    }
}

